package com.ossjk.qlh.system.service.impl;

import com.ossjk.core.util.CommonUtil;
import com.ossjk.qlh.system.entity.Permission;
import com.ossjk.qlh.system.entity.RolePermission;
import com.ossjk.qlh.system.mapper.RolePermissionMapper;
import com.ossjk.qlh.system.service.IRolePermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author chair
 * @since 2020-04-02
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

	public boolean authorityByRid(String rid, List<String> pids) {
		// 根据Rid删除所有权限
		QueryWrapper<RolePermission> queryWrapper = new QueryWrapper();
		queryWrapper.eq("rid", rid);
		baseMapper.delete(queryWrapper);
		// 批量新增所有权限
		if (!CommonUtil.isBlank(pids)) {
			List<RolePermission> rolePermissions = new ArrayList<RolePermission>();
			for (String pid : pids) {
				RolePermission rolePermission = new RolePermission();
				rolePermission.setRid(rid);
				rolePermission.setPid(pid);
				rolePermissions.add(rolePermission);
			}
			return this.saveBatch(rolePermissions);
		}

		return true;
	}

	@Override
	public List<String> queryPidByRid(Serializable rid) {

		return baseMapper.selectPidByRid(rid);
	}
}
